import os
import random
import subprocess
import numpy as np
from matplotlib import pyplot as plt
lines = []

ans = [[], [], []]

count = 1
up = 14

for i in range(up):
    temp = [[], [], []]
    for j in range(count):
        with open("openmp.cpp", "r") as f:
            lines = f.readlines()
        f.close()

        with open("openmp.cpp", "r+") as f:
            lines[5] = "#define N " + str(2**i) + "\n"
            lines[-1] = '}' + '\n'
            f.writelines(lines)
        f.close()
        print(lines[-1])

        os.system("g++ openmp.cpp -o openmp -fopenmp -O2")

        result = os.popen("openmp.exe")
        result_lines = result.readlines()
        for i in range(len(result_lines)):
            num = result_lines[i].split(": ")[1][:-3]
            temp[i].append(eval(num))

    for j in range(3):
        ans[j].append(np.mean(temp[j]))

    print(ans)

# x = [i for i in range(up)]
# # plt.plot(x, ans[0], 'r--', label='serial')
# # plt.plot(x, ans[1], 'g--', label='sse first')
# # plt.plot(x, ans[2], 'b--', label='sse second')
# plt.xlabel("log N")
# plt.ylabel("log time/ms")
# plt.legend()
# plt.show()